package com.catl.plc.master.wrapper.jssc;

import com.serotonin.modbus4j.serial.SerialPortWrapper;
import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;
import lombok.Data;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.io.OutputStream;

@Data
public class SerialPortWrapperImpl implements SerialPortWrapper {
    private static final Logger LOG = LoggerFactory.getLogger(SerialPortWrapperImpl.class);
    private SerialPort port;
    // 目标端口 例如 COM1--> COM2 ,commPortId = COM2,自己就是 COM1
    private String commPortId = "COM2";
    //波特率
    private int baudRate = SerialPort.BAUDRATE_9600;
    //数据位
    private int dataBits = SerialPort.DATABITS_8;
    //停止位
    private int stopBits = SerialPort.STOPBITS_1;
    //校验位
    private int parity = SerialPort.PARITY_NONE;
    //
    private int flowControlIn = SerialPort.FLOWCONTROL_NONE;
    //
    private int flowControlOut = SerialPort.FLOWCONTROL_NONE;

    public SerialPortWrapperImpl(String commPortId, int baudRate, int dataBits, int stopBits,
                                 int parity, int flowControlIn, int flowControlOut) {

        this.commPortId = commPortId;
        this.baudRate = baudRate;
        this.dataBits = dataBits;
        this.stopBits = stopBits;
        this.parity = parity;
        this.flowControlIn = flowControlIn;
        this.flowControlOut = flowControlOut;
        port = new SerialPort(this.commPortId);
    }

    public SerialPortWrapperImpl(String commPortId) {
        this.commPortId = commPortId;
        port = new SerialPort(this.commPortId);

    }

    @Override
    public void close() throws Exception {
        port.closePort();
    }

    @Override
    public void open() {
        try {
            port.openPort();
            port.setParams(this.getBaudRate(), this.getDataBits(), this.getStopBits(), this.getParity());
            port.setFlowControlMode(this.getFlowControlIn() | this.getFlowControlOut());
            port.addEventListener(new SerialPortEventListener() {
                @Override
                public void serialEvent(SerialPortEvent serialPortEvent) {

                }
            },1);
        } catch (SerialPortException ex) {
            LOG.error("Error opening port : {} for {} ", port.getPortName(), ex);
        }
    }

    @SneakyThrows
    @Override
    public InputStream getInputStream() {
        return new SerialInputStream(port);
    }

    @SneakyThrows
    @Override
    public OutputStream getOutputStream() {
        return new SerialOutputStream(port);
    }

    @Override
    public int getBaudRate() {
        return baudRate;
    }

    @Override
    public int getFlowControlIn() {
        return flowControlIn;
    }

    @Override
    public int getFlowControlOut() {
        return flowControlOut;
    }

    @Override
    public int getDataBits() {
        return dataBits;
    }

    @Override
    public int getStopBits() {
        return stopBits;
    }

    @Override
    public int getParity() {
        return parity;
    }


}
